<template>
  <el-dialog title="批量转账" :visible.sync="dialogVisible" width="680px">
    <div>
      <p style="font-size: 12px;">转出账户：{{ formdata.companyName }}({{ formdata.card }})</p>
      <p style="margin-top: 8px;font-size: 14px;font-weight: bold;">余额（元）：{{ formdata.amt }}</p>
    </div>
    <p style="margin-top: 24px;font-size: 14px;font-weight: bold;">收款账户</p>
    <div>
      <el-table v-loading="loading" :data="dataList">
        <el-table-column label="会员名称" prop="memberName" align="center" />
        <el-table-column label="卡号" prop="card" align="center" />
        <el-table-column label="账户余额" prop="amt" align="center" />
        <el-table-column label="转入金额" prop="value" align="center">
          <template slot-scope="scope">
            <el-form :model="scope.row" inline>
              <el-form-item prop="value">
                <el-input v-model="scope.row.value" />
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="deletFn(scope.row, scope.$index)">移除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelFn">取 消</el-button>
      <el-button type="primary" @click="saveFn" v-loading="saveLoading">确 定</el-button>
    </div>
  </el-dialog>

</template>
<script>
  import {
    cmpDetail,
    sub_card,
    transfer
  } from '@/api/fleet'
  import {
    validator,
    isFloat
  } from '@/utils/validate';

  export default {
    data() {
      return {
        formdata: {},
        saveLoading: false,
        dialogVisible: false,
        rules: {},
        dataList: [],
        loading: false,
      }
    },
    methods: {
      open(info) {
        if (info) {
          this.formdata = info
          this.getCmpDetail()
        }
        this.$nextTick(() => {
          this.dialogVisible = true;
        })
      },
      getCmpDetail() {
        cmpDetail().then(response => {
          this.formdata = response.data;
          this.getMemberList();
        })
      },
      getMemberList() {
        this.loading = true;
        const model = {
          status: 'normal',
          memberType: "sub",
          pageNum: 1,
          pageSize: 9999,
          companyId: this.formdata.companyId
        }
        try {
          sub_card(model).then(res => {
            if (res.code === 200 && res.data.rows) {
              this.dataList = res.data.rows.map(item => {
                return {
                  memberId: item.memberId,
                  memberName: item.memberName,
                  card: item.card,
                  amt: item.amt,
                  value: 0
                }
              });
            }
          })
        } finally {
          this.loading = false;
        }
      },
      cancelFn() {
        this.dialogVisible = false;
      },
      saveFn() {
        const model = {
          companyId: this.formdata.companyId,
          inList: []
        }
        for (let item of this.dataList) {
          if (!isFloat(item.value) || parseFloat(item.value) < 0) {
            this.$message.error(`${item.memberName} 如需转账金额需大于0`);
            return
          }
          let transVal = parseFloat(item.value)
          if (transVal > 0) {
            model.inList.push({
              memberId: item.memberId,
              value: transVal
            })
          }
        }
        if (model.inList.length == 0) {
          this.$message.error('请输入转账金额');
          this.saveLoading = false;
          return
        }
        try {
          this.saveLoading = true;
          transfer(model).then(res => {
            if (res.code === 200) {
              this.$message.success('操作成功');
              this.$emit('on-success')
              this.cancelFn()
            }
          })
        } finally {
          this.saveLoading = false;
        }
      },
      deletFn(data, index) {
        this.dataList.splice(index, 1);
      }
    }
  }
</script>
<style lang="scss" scoped>
</style>
